<?php
if (! defined ('IN_ZWS')) exit ();

class BAction extends PublicAction {
	function index() {
		$M = M('GroupGroup');
		$firstGroup = $M -> where(1) -> find();
		header('Location:' . __APP__ . '/b/g/' . $firstGroup['id']);
	}

	function g() {
		$M = M('GroupGroup');
		$groupId = intval($_GET["_URL_"][2]);
		if (!$groupId) {
			$firstGroup = $M -> where(1) -> find();
			$groupId = $firstGroup['id'];
		} else {
			if (!$M -> where('id=' . $groupId) -> find()) $this -> error('抱歉，URL错误', __APP__ . '/b');
		}
		$groups = $M -> select();
		$grades = $M -> table('zws_group_grade') -> where('gid=' . $groupId) -> select();
		$subjects = $M -> table('zws_group_subject') -> select();
		$cate = $M -> table('zws_group_category') -> where('res=0') -> select();
		$bookList = $M -> table('zws_article') -> where('res=0 AND gpid=' . $groupId) -> select();
		$this -> assign('groupId', $groupId);
		$this -> assign('groups', $groups);
		$this -> assign('grades', $grades);
		$this -> assign('subjects', $subjects);
		$this -> assign('cate', $cate);
		$this -> assign('bookList', $bookList);
		$this -> display();
	}

	function r() {
		$aid = intval($_GET["_URL_"][2]);
		$article = D('ArticleView');
		$book = $article -> where(array('Article.res' => 0, 'Article.id' => $aid,)) -> find();
		if ($book) {
			$this -> assign('book', $book);
			$article = D('CommentView'); //实例化Comment视图模型
			$articleComments = $article -> where(array('aid' => $aid, 'pid' => 0)) -> limit(10) -> order('ctime') -> select(); //将一级评论查询出来
			foreach($articleComments as $comment) {
				$comments[$comment['id']] = $comment; //将查询出来的一级评论按id赋给comments数组
			}
			$commentID = array_keys($comments); //取出一级评论的id，为下面查询对应的二级评论做准备
			if (!isEmpty($commentID)) {
				foreach($commentID as $id) {
					$sqlArray[] = 'pid=' . $id; //生成查询二级评论所用条件数组
				}
				$sql = implode(' OR ', $sqlArray); //将数组连接，生成对应的SQL条件语句
				$articleComments = $article -> where($sql) -> limit(10) -> order('ctime') -> select(); //查询对应二级评论
				foreach($articleComments as $comment) {
					$comments[$comment['pid']]['comments'][$comment['id']] = $comment; //按树形排列评论
					$applaudCommentID[] = $comment['id'];
				}
				$applaudCommentID = array_merge($commentID, $applaudCommentID); //取出一二级评论的ID
				$applaud = D('CommentApplaudView'); //实例化CommentApplaud视图模型
				unset($sqlArray); //将之前的sql合并前数组销毁
				foreach($applaudCommentID as $id) {
					$sqlArray[] = 'ArticleComment.id=' . $id; //生成新的用于合并的id子条件数组
				}
				$sql = implode(' OR ', $sqlArray); //生成新的sql条件字句
				$applaudArray = $applaud -> where($sql) -> select(); //找出各评论的赞
				foreach($applaudArray as $applaudId) {
					$applauds[$applaudId['id']][] = $applaudId['uid']; //将找出来的信息按评论ID分组
				}
			}
			$this -> assign('comments', $comments);
			$this -> assign('applauds', $applauds);
			$M = M('ArticleComment');
			$count['comment'] = $M -> where(array('aid' => $aid,)) -> count();
			if (!$M -> table('zws_article_view') -> where(array('uid' => $_SESSION['uid'], 'aid' => $aid)) -> find()) {
				$M -> table('zws_article_view') -> add(array('uid' => $_SESSION['uid'], 'aid' => $aid, 'vtime' => time()));
			}
			$count['view'] = $M -> table('zws_article_view') -> where(array('aid' => $aid)) -> count();
			$count['fav'] = $M -> table('zws_article_fav') -> where(array('aid' => $aid)) -> count();
			$isFav = $M -> table('zws_article_fav') -> where(array('uid' => $_SESSION['uid'], 'aid' => $aid)) -> find();
			$this -> assign('count', $count);
			$this -> assign('isFav', $isFav?true:false);
			$groups = $M -> table('zws_group_group') -> select();
			$this -> assign('groups', $groups);
			$this -> assign('articleId', $aid);
		} else {
			$this -> error('抱歉，没有找到该教辅！', __APP__ . '/b');
		}
		$this -> display();
	}
}
